<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event,Auth;
class JianchaController extends Controller
{

    public function index(request $request)
    {
        if($request->rows) {
            $map = array();
            if(Auth::guard('admin')->user()->dengji == 2){
                $map['units.city'] = Auth::guard('admin')->user()->city;
            }
            if(Auth::guard('admin')->user()->dengji == 3){
                $map['units.area'] = Auth::guard('admin')->user()->area;
            }
            if(Auth::guard('admin')->user()->dengji == 4){
                $map['units.org_id'] = Auth::guard('admin')->user()->org_id;
                $map['units.minjing'] = Auth::guard('admin')->user()->id;
            }
            //如果是企业用户
            if(Auth::guard('admin')->user()->role_id == 22){
                $map['units.id'] = Auth::guard('admin')->user()->unit_id;
            }
            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['units.city'] = $request->city;
                }
            }
            if($request->area){
                $map['units.area'] = $request->area;
            }
            if($request->org_id){
                $map['units.org_id'] = $request->org_id;
            }
            if($request->name){
                $map[] = array('units.name','like','%'.$request->name.'%');
            }
            if($request->input('type') != null){
                $map['units.type'] = $request->type;
            }
            if($request->input('xingzhi') != null){
                $map['units.xingzhi'] = $request->xingzhi;
            }
            if($request->input('jibie') != null){
                $map['units.is_imp'] = $request->jibie;
            }
            if($request->input('is_del') != null){
                $map['units.is_del'] = $request->is_del;
            }
            $res = DB::table('units')
                ->where($map)
                ->leftJoin('area as a', 'units.city', '=', 'a.id')
                ->leftJoin('area as b', 'units.area', '=', 'b.id')
                ->leftJoin('area as c', 'units.org_id', '=', 'c.id')
                ->leftJoin('unit_type as d', 'units.type', '=', 'd.id')
                ->leftJoin('admin_users as e', 'units.user_id', '=', 'e.id')
                ->orderBy('units.city','asc')
                ->orderBy('units.area','asc')
                ->orderBy('units.org_id','asc')
                ->orderBy('units.is_imp','asc')
                ->orderBy('units.type','asc')
                ->orderBy('units.xingzhi','asc')
                ->select('units.*', 'a.name as city', 'b.name as area', 'c.name as org_id', 'd.name as type', 'e.name as user_id')
                ->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                if($v->is_imp == 10){
                    $list[$k]->is_imp = '省级重点单位';
                }elseif($v->is_imp == 20){
                    $list[$k]->is_imp = '市级重点单位';
                }elseif($v->is_imp == 30){
                    $list[$k]->is_imp = '县区级重点单位';
                }elseif($v->is_imp == 40){
                    $list[$k]->is_imp = '一般单位';
                }

                if($v->xingzhi == 10){
                    $list[$k]->xingzhi = '国有';
                }elseif($v->xingzhi == 20){
                    $list[$k]->xingzhi = '民营';
                }elseif($v->xingzhi == 30){
                    $list[$k]->xingzhi = '股份';
                }elseif($v->xingzhi == 40){
                    $list[$k]->xingzhi = '合资';
                }elseif($v->xingzhi == 50){
                    $list[$k]->xingzhi = '外资';
                }elseif($v->xingzhi == 60){
                    $list[$k]->xingzhi = '个体';
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            //类型
            $type = db::table('unit_type')->where('is_del','0')->get();
            $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();//市
            return view('admin.jiancha.index', ['type'=>$type,'city' => $city]);
        }
    }

    public function detail($id,Request $request)
    {
        $re = DB::table('units')->where('id',$id)->first();
        if($request->isMethod('post')){
            $obj = DB::table('units');
            $data = Input::except('_token');
            if(!Input::get('type')){
                return ['status'=>0,'msg'=>'请选择单位类型'];
            }
            if($data['org_id']){
                $area = DB::table('area')->where('id','=',$data['org_id'])->value('cid');
                if($area != $data['area']){
                    return ['status'=>0,'msg'=>'系统错误，请重新操作'];
                    /*echo "<script>alert('系统错误，请重新操作');window.history.go(-1);</script>";
                    exit();*/
                }
            }else{
                $data['org_id'] = $re->org_id;
                $data['area'] = $re->area;
                $data['city'] = $re->city;
            }

            if($data['area']){
                $city = DB::table('area')->where('id','=',$data['area'])->value('cid');
                if($city != $data['city']){
                    return ['status'=>0,'msg'=>'系统错误，请重新操作'];
                    /*echo "<script>alert('系统错误，请重新操作');window.history.go(-1);</script>";
                    exit();*/
                }
            }else{
                $data['area'] = $re->area;
            }

            if(!$data['city']){
                $data['city'] = $re->city;
            }
            //上传图片
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $result = $obj->where('id',$id)->update($data);
            //如果内容发生改变
            /*if($result == 1){
                $data['updated_at'] = date('Y-m-d H:i:s',time());
                $data['user_id'] = Auth::guard('admin')->user()->id;
                $obj->where('id',$id)->update($data);
                $data['unit_id'] = $re->id;
                db::table('units_history')->insert($data);
            }*/
            return ['status'=>1,'msg'=>'编辑成功'];
            //return redirect('admin/units/index/');
        }
        //单位类型
        $type = db::table('unit_type')->get();
        //法人信息
        $faren = db::table('faren')->where('unit_id',$id)->orderby('id','desc')->first();
        //要害部位数量
        $buwei_num = db::table('zhongyaobuwei')->where('unit_id',$id)->count();
        //要害部位人员数量
        $gzry_num = db::table('gongzuorenyuan')->where('unit_id',$id)->count();
        return view('admin.jiancha.detail', ['re' => $re,'type' => $type,'id' => $id,'faren'=>$faren,'buwei_num'=>$buwei_num,'gzry_num'=>$gzry_num]);
    }

    public function jiancha_list($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('jiancha')->where($map)->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                $list[$k]->user = getValue($v->user_id,'admin_users' ,'id' , 'name');
                if($v->status == 1){
                    $list[$k]->status = '合格';
                }elseif($v->status == 2){
                    $list[$k]->status = '不合格';
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.jiancha.jiancha_list', ['id'=>$id]);
        }
    }

    public function add_jiancha(Request $request,$id)
    {
        $type = db::table('units')->where('id',$id)->value('type');
        $re = db::table('units')->where('id',$id)->first();
        //安全检查项
        $aqjc_count = db::table('unit_type_aqjc')->where('type_id',$type)->where('is_show','1')->count();
        if($aqjc_count > 0){
            $aqjc = db::table('unit_type_aqjc')->where('type_id',$type)->where('is_show','1')->orderby('paixu','asc')->get();
            foreach ($aqjc as $k=>$v){
                if($v->field_type == 2){
                    $v->daan = db::table('unit_type_aqjc_daan')->where('pid',$v->id)->get();
                }
            }
        }else{
            $aqjc = array();
        }
        if($request->isMethod('post')){
            $data = Input::except('_token','aqjc');
            $val = Input::get('aqjc');

            //检查项不能为空
            /*if(!Input::get('user_name')){
                return ['status'=>0,'msg'=>'请填写检查人'];
            }

            if(!Input::get('add_time')){
                return ['status'=>0,'msg'=>'请填写检查时间'];
            }

            if(!Input::get('fuzeren_name')){
                return ['status'=>0,'msg'=>'请填写单位负责人'];
            }

            if(!Input::get('fuzeren_phone')){
                return ['status'=>0,'msg'=>'请填写负责人电话'];
            }*/
            if(!$val){
                return ['status'=>0,'msg'=>'请先添加检查项'];
            }

            if(count($val) != $aqjc_count){
                return ['status'=>0,'msg'=>'请将检查项填写完整'];
            }

            if(!Input::get('status')){
                return ['status'=>0,'msg'=>'请选择最终检查结果'];
            }

            $unit_info = db::table('units')->where('id',$id)->first();
            if($unit_info->org_id){
                $data['city'] = $unit_info->city;
                $data['area'] = $unit_info->area;
                $data['org_id'] = $unit_info->org_id;
                $data['unit_name'] = $unit_info->name;
            }else{
                return ['status'=>0,'msg'=>'请先完善单位市区派出所信息'];
            }
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['mj_id'] = $unit_info->minjing;
            $data['type'] = $type;
            $data['aqjc_type'] = '1';
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            if($unit_info->minjing == Auth::guard('admin')->user()->id){
                $data['is_self'] = '1';
            }else{
                $data['is_self'] = '2';
            }
            $jiancha_id = DB::table('jiancha')->insertGetId($data);

            //处理检查表数据
            foreach ($val as $k=>$v){
                $where['field_name'] = $k;
                $where['type_id'] = $type;

                //$array['unit_id'] = $id;//企业id
                $array['jiancha_id'] = $jiancha_id;//检查id
                //$array['type_id'] = $type;//企业类型
                //$array['aqjc_type'] = $k;//检查类型
                //$array['field_id'] = db::table('unit_type_aqjc')->where($where)->value('id');//字段id;
                $array['field_content'] = db::table('unit_type_aqjc')->where($where)->value('field_content');//字段内容
                $array['field_name'] = $k;//字段名
                $array['val'] = $v;//字段值
                db::table('unit_type_aqjc_value')->insert($array);
            }

            if($jiancha_id){
                return ['status'=>1,'msg'=>'添加成功'];
            }else{
                return ['status'=>0,'msg'=>'添加失败'];
            }
            /*if(Input::get('status') == 2){
                return redirect('admin/case/create/'.$jiancha_id);
            }*/
            //return redirect('admin/units/jiancha/'.$id);
        }
        return view('admin.jiancha.add_jiancha',['re'=>$re,'type' => $type,'id'=>$id,'aqjc'=>$aqjc]);
    }

    public function edit_jiancha($id,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('jiancha')->where('id',$id)->update($data);
            if($re){
                return ['status'=>1,'msg'=>'编辑成功'];
            }else{
                return ['status'=>0,'msg'=>'编辑失败'];
            }
            //$cid = DB::table('jiancha')->where('id',$id)->value('unit_id');
            //return redirect('admin/units/jiancha/'.$cid);
        }
        $re = DB::table('jiancha')->where('id',$id)->first();
        $type = db::table('jiancha')->where('id',$id)->value('type');
        //检查结果
        $res = DB::table('unit_type_aqjc_value')->where('jiancha_id',$re->id)->get();
        return view('admin.jiancha.edit_jiancha', ['res'=>$res,'re' => $re,'type' => $type,'id'=>$id]);
    }

    public function del_jiancha($id)
    {
        $re = db::table('jiancha')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function print_jiancha($id)
    {
        $re = DB::table('jiancha')->where('id',$id)->first();
        $type = db::table('jiancha')->where('id',$id)->value('type');
        return view('admin.jiancha.print_jiancha', ['re' => $re,'id'=>$id,'type'=>$type]);
    }
}
